package cn.fjc920.siaa.adapters

import cn.fjc920.siaa.fragments.MyDialogFragment
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.fjc920.siaa.R
import cn.fjc920.siaa.activityutils.Contexts
import cn.fjc920.siaa.itembean.Hours

class HoursAdapter(private val hoursList: ArrayList<Hours>, val isHide: Boolean) :
    RecyclerView.Adapter<HoursAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        val userDataView: LinearLayout = view.findViewById(R.id.hours_item_userData)
        val userView: TextView = view.findViewById(R.id.hours_item_user)
        val doorView: TextView = view.findViewById(R.id.hours_item_door)
        val dateView: TextView = view.findViewById(R.id.hours_item_date)
        val projectView: TextView = view.findViewById(R.id.hours_item_project)
        val typeView: TextView = view.findViewById(R.id.hours_item_type)
        val timeView: TextView = view.findViewById(R.id.hours_item_time)
        val remarksView: TextView = view.findViewById(R.id.hours_item_remarks)
        val rootView: LinearLayout = view.findViewById(R.id.hours_item_root)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.hours_item, parent, false)
        val holder = ViewHolder(view)
        holder.itemView.setOnClickListener {
            val hours = hoursList[holder.adapterPosition]
            MyDialogFragment(hours.editUrl, hours.deleteUrl, hours._csrf, hours.hoursId,
            "姓名：${hours.user}\n部门：${hours.door}\n日期：${hours.date}\n工作类型：${hours.type}" +
                    "\n工作时长：${hours.time}\n备注信息：${hours.remarks}\n所属项目：${hours.project}").show(
                Contexts.homePageActivity.supportFragmentManager,
                "更多操作"
            )
        }
        return holder
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val hours = hoursList[position]

        val dateData:StringBuilder = StringBuilder(hours.date.substring(5).replace("-","月")).append("日")
        if (dateData[3] == '0'){
            dateData.replace(3,4,"")
        }
        if (dateData[0] == '0'){
            dateData.replace(0,1,"")
        }

        if (!isHide) {
            holder.userDataView.visibility = View.VISIBLE
            holder.userView.text = hours.user
            holder.doorView.text = hours.door
        }
        holder.dateView.text = dateData
        holder.projectView.text = hours.project
        holder.typeView.text = hours.type
        holder.timeView.text = hours.time
        holder.remarksView.text = hours.remarks
        if (!isHide) {
            holder.rootView.contentDescription =
                "${hours.user}, ${hours.door}, ${dateData}, " +
                        "类型, ${hours.type}, 时长, ${hours.time}, 备注, ${hours.remarks}, 项目, ${hours.project}"
        } else {
            holder.rootView.contentDescription = "${dateData}, " +
                    "类型, ${hours.type}, 时长, ${hours.time}, 备注, ${hours.remarks}, 项目, ${hours.project}"
        }

    }

    override fun getItemCount() = hoursList.size

    @SuppressLint("NotifyDataSetChanged")
    fun loadMore(list: ArrayList<Hours>, refresh: Boolean) {
        hoursList.addAll(list)
        if (refresh) {
            notifyDataSetChanged()
        } else {
            notifyItemInserted(itemCount)
        }
    }

    fun clearData() {
        hoursList.clear()
    }

}